package com.jiwei.system.serviceImpl;

import com.jiwei.system.dao.MemberMapper;
import com.jiwei.system.pojo.Member;
import com.jiwei.system.pojo.MemberLogin;
import com.jiwei.system.service.AdminOperateMember;
import com.jiwei.system.service.MemberInfoService;
import com.jiwei.system.vo.Login;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import com.jiwei.system.dao.MemberLoginMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service("MemberService")
public class MemberServiceImpl implements MemberInfoService, AdminOperateMember {

    @Resource
    private MemberLoginMapper memberLoginMapper;

    @Resource
    private MemberMapper memberMapper;


    @Override
    public Boolean verifyMember(MemberLogin login) {
          //获取到加密之后的密码
        String account = login.getAccount();
        String password = login.getPasswordMD5();
        Login newlogin = new Login(account,password);
        login = newlogin.changeToPo();
        System.out.print("加密后的密码为:");
        System.out.println(login.getPasswordMD5());
        MemberLogin getMemberLogin = memberLoginMapper.queryMemberLoginByAccount(account);
        return login.equals(getMemberLogin);

    }

    @Override
    public Member getMyInfo(String account) {
        return memberMapper.queryMemberByAccount(account);
    }

    @Override
    public Boolean changePassword(String account, String password, String newPassword) {
        MemberLogin memberLogin = memberLoginMapper.queryMemberLoginByAccount(account);
        if(DigestUtils.md5Hex(password).equals(memberLogin.getPasswordMD5())){
            memberLogin.setPasswordMD5(DigestUtils.md5Hex(newPassword));
            memberLoginMapper.updateMemberLogin(memberLogin);
            return true;
        }
        else
            return false;
    }

    @Override
    public void updatePersonalInfo(Member member) {
        memberMapper.update(member);
    }

    @Override
    public String getMemberRoleByID(String account) {
        return memberLoginMapper.queryRoleById(account);
    }

    @Override
    public List<Member> getAllMemberList() {

        return memberMapper.queryAllMember();
    }

    @Override
    public List<Member> getMemberListByRole(int page, int limit, String role) {
        int start = (page - 1) * limit;
        return memberMapper.queryMemberByRole(start,limit,role);
    }

    @Override
    public void resetPassword(String account) {
        String Pwd = DigestUtils.md5Hex("000000");
        memberMapper.resetPassword(account,Pwd);
    }

    @Override
    public void deleteMember(String account) {
        memberMapper.deleteMember(account);
    }

    @Override
    public Boolean changeRole(String account, String role) {
        HashMap<String, Integer> map = new HashMap<String, Integer>() {
            {
                put("队员", 0);
                put("接线员", 1);
                put("管理员", 2);
            }
        };
        String oldRole = memberLoginMapper.queryRoleById(account);
        if(role.equals("管理员")||oldRole.equals("管理员")||map.get(oldRole)==map.get(role)){
            return false;
        }else{
            memberMapper.changeMemberRole(account,role);
            return true;
        }

    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void addMember(String account, String name) {
        Member member = new Member();
        member.setAccount(account);
        member.setName(name);
        memberMapper.addMemberInfo(member);
        String PwdMD5 = DigestUtils.md5Hex("000000");
        String role = "队员";
        memberLoginMapper.addMemberLogin(account,PwdMD5,role);
    }
}
